;(function($)  {

    function Upload() {}

    Upload.prototype.init = function (options) {
        this.setting = $.extend({
            uploadEle: null,
            uploadUrl: '/',
            defaultImage: '/images/image.png',
            mimeTypes: [],
            maxSize: 10485760,
            afterSelected: null,
            uploadPath: 'default',
            uploadSuccess: null,
            uploadError: null,
            multiple: null,
        }, options);
        return this;
    };

    Upload.prototype.click = function(){
        var _self = this;

        $(document).on('click','#'+this.setting.uploadEle,function () {

            var uploadInput = $('<input type="file" style="display: none" '+(_self.setting.multiple||'')+' accept="'+_self.setting.mimeTypes.toString()+'" />');

            uploadInput.change(function(e){

                var files = e.target.files;

                for (var index=0 ;index < files.length;index++){

                    var file = files[index];

                    if(!_self.beforeRequest(file)){
                        continue;
                    }

                    if(!_self.afterSelected(file)){
                        continue;
                    }

                    var uploadForm = new FormData();
                    uploadForm.append("file",file);
                    uploadForm.append("uploadPath",_self.setting.uploadPath);
                    _self.request(uploadForm);
                }
            });
            uploadInput.val('').click();
            _self.setting.uploadEle = $(this);
        });

        return this;

    };

    Upload.prototype.afterSelected = function(file){
        if(typeof this.setting.afterSelected === "function"){
            return this.setting.afterSelected(file,this)
        }
        return true;
    };

    Upload.prototype.beforeRequest = function(file){
        if($.inArray(file.type.toLowerCase(),this.setting.mimeTypes) < 0) {
            layer.msg('上传文件上传格式错误.');
            return false;
        }

        if(this.setting.maxSize && file.size > this.setting.maxSize) {
            var showErrorSize = this.setting.maxSize/1024/1024;
            if(showErrorSize<1){
                showErrorSize = this.setting.maxSize/1024+'kb.';
            }else{
                showErrorSize += 'M.';
            }
            layer.msg('上传文件不能大于'+showErrorSize);
            return false;
        }
        return true;
    };

    Upload.prototype.request = function(uploadForm){

        var _self = this;

        $.ajax({
            type: "POST",
            url :  this.setting.uploadUrl,
            data: uploadForm,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (res) {
                layer.msg(res.message);
                if(typeof _self.setting.uploadSuccess === "function"){
                    _self.setting.uploadSuccess(res,_self);
                }
            },
            error: function (jqXHR) {
                layer.msg("上传文件失败");
                if(typeof _self.setting.ploadError === "function"){
                    _self.setting.uploadError(jqXHR,_self);
                }
            }
        });
    };

    window.Upload = Upload;

})(jQuery);